CROSS = 
CC = $(CROSS)gcc
AR = $(CROSS)ar

LIB = dicop
TESTSERV = testserver
TESTCLI = testclient

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LIB_SHARED = lib$(LIB).so
LIB_STATIC = lib$(LIB).a
else ifeq ($(UNAME_S),Darwin)
LIB_SHARED = lib$(LIB).dylib
LIB_STATIC = lib$(LIB).a
endif

INCDIR = /usr/include
ifeq ($(CROSS),)
LIBDIR = /usr/lib/libdi
else
LIBDIR = `pwd`/.lib
endif

CFLAGS = -fPIC -I `pwd` -I $(INCDIR) -ggdb `xml2-config --cflags` -Wno-int-to-pointer-cast
LDFLAGS = -L$(LIBDIR) -ldicom -lpthread `xml2-config --libs`
ifeq ($(UNAME_S),Linux)
LDFLAGS += -Wl,-rpath=$(LIBDIR)
LDFLAGS_SO = -shared
else ifeq ($(UNAME_S),Darwin)
LDFLAGS += -Wl,-rpath,'$(LIBDIR)'
LDFLAGS_SO = -dynamiclib -install_name '$(LIBDIR)/$(LIB_SHARED)' -current_version 1.0
endif

all: $(LIB) $(TESTSERV) $(TESTCLI)

%.o: %.c
	$(CC) -Wall -g -c $(CFLAGS) $< -o $@

$(LIB): $(LIB).o
	$(CC) $(LDFLAGS_SO) $(LDFLAGS) -o $(LIB_SHARED) $^
	$(AR) -r $(LIB_STATIC) $^

$(TESTSERV): $(LIB) $(TESTSERV).o
	$(CC) -o $@ $(TESTSERV).o $(LIB).o $(LDFLAGS)

$(TESTCLI): $(LIB) $(TESTCLI).o
	$(CC) -o $@ $(TESTCLI).o $(LIB).o $(LDFLAGS)

install:
	@mkdir -p $(INCDIR)/dicop
	cp dicop.h $(INCDIR)/dicop/
	@mkdir -p $(LIBDIR)
	cp $(LIB_SHARED) $(LIB_STATIC) $(LIBDIR)

clean:
	@rm -f *.o *.a *.so* *.dylib
	@rm -f $(LIB) $(TESTSERV) $(TESTCLI)
